package com.imau.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class ProfileResult
{
    private String id;

    private String mobile;

    private String username;

    private Map roles;

    public ProfileResult(User user,Map rolesMap)
    {
        this.mobile = user.getMobile();

        this.username = user.getUsername();

        this.id = user.getId();

        this.roles = rolesMap;

    }

    public ProfileResult(User user)
    {

        this.mobile = user.getMobile();

        this.username = user.getUsername();

        this.id = user.getId();

        //角色数据
        Set<Permission> menus = new HashSet<>();
        Set<Permission> points = new HashSet<>();
        Set<Permission> apis = new HashSet<>();

        Map rolesMap = new HashMap<>();

        for (Role role : user.getRoles())
        {
            for (Permission perm : role.getPermissions()) {

                String code = perm.getCode();

                if (perm.getType() == 1) {
                    menus.add(perm);

                } else if (perm.getType() == 2) {
                    points.add(perm);

                } else {
                    apis.add(perm);
                }
            }
        }

        rolesMap.put("menus", menus);
        rolesMap.put("points", points);
        rolesMap.put("apis", apis);

        this.roles = rolesMap;

    }
}


